<?php
$this->breadcrumbs = array(
	'All Users' => array('users/index'),
	$user->name => array('users/view', 'id' => $user->id),
	'Edit User'
);
?>
<script type="text/javascript">
	var userId = <?php echo $user->id; ?>;
	var selectedOverride;
</script>
<?php $this->renderPartial('_tabs', array('user' => $user)); ?>
<?php
$form = $this->beginWidget('PsActiveForm', array(
	'id' => 'add-team-form',
	'enableAjaxValidation' => false,
	'htmlOptions' => array(
		'class' => 'form'
	)
		));
?>
<div class="grid_8">
	<div class="section">
		<div class="block">
			<div class="form">
				<h2>User Details</h2>
				<div class="table-block">
					<table class="form-table">
						<?php if (Yii::app()->user->checkModuleAccess("Propsage.Accounting.Commission")): ?>
							<tr>
								<th scope="row" width="180px">
									<?php echo $form->labelEx($user, 'upline_user_id', array('label' => 'Upline')); ?>
								</th>
								<td>
									<div class="form-table-block">
										<?php
										$this->widget('common.widgets.PsSelect2.PsSelect2', array(
											'model' => $user,
											'attribute' => 'upline_user_id',
											'data' => $otherUsersListData,
											'htmlOptions' => array(
												'prompt' => 'No Upline',
												'multiple' => false,
												'style' => 'width:300px;',
												'data-placeholder' => 'Select user'
											)
										));
										?>
										<?php echo $form->error($user, 'upline_user_id'); ?>
									</div>
								</td>
							</tr>
							<tr>
								<th scope="row" width="180px">
								</th>
								<td>
									<p class="hint">The upline (also known as Tier 1) is the user who directly receives the overriding commission from this user (<?php echo $user->name; ?>).</p>
									<span id="tiers"></span>
								</td>
							</tr>
							<tr>
								<th scope="row" width="180px">
									<?php echo $form->labelEx($user, 'recommender_user_id', array('label' => 'Referred By')); ?>
								</th>
								<td>
									<div class="form-table-block">
										<?php
										$this->widget('common.widgets.PsSelect2.PsSelect2', array(
											'model' => $user,
											'attribute' => 'recommender_user_id',
											'data' => $otherUsersListData,
											'htmlOptions' => array(
												'prompt' => 'No Referrer',
												'multiple' => false,
												'style' => 'width:300px;',
												'data-placeholder' => 'Select user'
											)
										));
										?>
										<?php echo $form->error($user, 'recommender_user_id'); ?><span id="tiers"></span>
									</div>
								</td>
							</tr>
							<tr>
								<th scope="row" width="180px">
								</th>
								<td>
									<p class="hint">The referrer is a salesperson who referred this user to join the company.  The referrer may not necessarily be the upline. If set in the commission policy, a referrer may receive a percentage of the user's commissions (deducted from Tier 1).</p>
								</td>
							</tr>
							<tr>
								<th scope="row" width="150">
									<?php echo GxHtml::submitButton('Save', array('name' => 'submit', 'class' => 'green link-button no-margin')); ?>
								</th>
								<td></td>
							</tr>
						<?php endif; ?>
					</table>
				</div>
			</div>
		</div>
	</div>
</div>
<?php $this->endWidget(); ?>

<?php
$form = $this->beginWidget('PsActiveForm', array(
	'id' => 'assign-structure-form',
	'enableAjaxValidation' => false,
	'htmlOptions' => array(
		'class' => 'form'
	)
		));
?>
<?php if (Yii::app()->user->checkModuleAccess('Propsage.Accounting.Commission')): ?>
	<div class="grid_8">
		<div id="user-info" class="section">
			<div class="block">
				<div class="form">
					<h2>Commission Structures</h2>
					<?php if ($user->agency->hasParentAgency()): ?>
						<h4><i>By parent agency <?php echo $user->agency->parent->name; ?></i></h4>
						<table id="commission_structures_parent" class="form-table">
							<tr>
								<th scope="row" width="24"></th>
								<th scope="row" width="150">Name </th>
								<th scope="row">Type </th>
							</tr>
							<?php if (!empty($parentStructures)): ?>
								<?php foreach ($parentStructures as $structure): ?>
									<tr>
										<td>
											<input type="checkbox" 
												   name="structureIds[]" 
												   value="<?php echo $structure->id; ?>"
												   <?php echo(in_array($structure->id, $assignedStructureIds) ? 'checked' : ''); ?>
												   />
										</td>
										<td><?php echo $structure->name; ?></td>
										<td><?php echo $structure->commissionStructureTypeCode->description; ?></td>
									</tr>
								<?php endforeach; ?>
							<?php else: ?>
								<tr><td colspan="3">None</td></tr>
							<?php endif; ?>
							<tr><td colspan="3"></td></tr>
						</table>
					<?php endif; ?>
					<h4><i>By own agency <?php echo $user->agency->name; ?></i></h4>
					<table id="commission_structures_own" class="form-table">
						<tr>
							<th scope="row" width="24"></th>
							<th scope="row" width="150">Name </th>
							<th scope="row">Type </th>
						</tr>
						<?php if (!empty($ownStructures)): ?>
							<?php foreach ($ownStructures as $structure): ?>
								<tr>
									<td>
										<input type="checkbox" 
											   name="structureIds[]" 
											   value="<?php echo $structure->id; ?>"
											   <?php echo(in_array($structure->id, $assignedStructureIds) ? 'checked' : ''); ?>
											   />
									</td>
									<td><?php echo $structure->name; ?></td>
									<td><?php echo $structure->commissionStructureTypeCode->description; ?></td>
								</tr>
							<?php endforeach; ?>
						<?php else: ?>
							<tr><td colspan="3">None</td></tr>
						<?php endif; ?>
						<tr><td colspan="3"></td></tr>
					</table>
					<div class="button-row">
						<?php echo GxHtml::submitButton('Save', array('name' => 'submit', 'class' => 'green link-button no-margin')); ?>
					</div>
				<?php else: ?>
					<?php echo 'No commissions structures created.'; ?>
					<br/>
					<br/>
				<?php endif; ?>
			</div>
		</div>
	</div>
</div>

<?php $this->endWidget(); ?>


<?php if (Yii::app()->user->checkModuleAccess('Propsage.Accounting.Commission')): ?>
	<div class="grid_16">
		<div id="user-info" class="section">
			<div class="block">
				<div class="form" model-class="Promotion">
					<h2>Appointment History</h2>
					<?php
					$this->widget('PsGridView', array(
						'id' => 'Promotion-grid',
						'dataProvider' => $promotionsDataProvider,
						'emptyText' => 'No Appointments Found',
						'template' => '{items}{pager}',
						'pagerCssClass' => 'pager float-right',
						'htmlOptions' => array(
							'class' => 'grid-view no-shadow'
						),
						'rowCssClassExpression' => '($row % 2 == 0 ? "even" : "odd")',
						'columns' => array(
							'id' => array(
								'header' => '',
								'name' => 'id',
								'type' => 'raw',
								'value' => 'CHtml::hiddenField("id", $data->id)',
								'headerHtmlOptions' => array(
									'width' => 5,
								)
							),
							'designation_rank' => array(
								'header' => 'Rank',
								'value' => '$data->designation->pos',
								'headerHtmlOptions' => array(
									'width' => 50,
									'class' => 'align-center',
								),
								'htmlOptions' => array(
									'class' => 'align-center',
								)
							),
							'designation_name' => array(
								'header' => 'Designation',
								'value' => '$data->designation->name',
								'headerHtmlOptions' => array(
									'width' => 250,
								),
							),
							'from_date' => array(
								'header' => 'Effective From',
								'value' => 'Yii::app()->dateFormatter->formatDateTime(strtotime($data->from_date), "short", null)',
								'headerHtmlOptions' => array(
									'width' => 100,
								),
							),
//							'promoted_time' => array(
//								'header' => 'Time Appointed',
//								'value' => 'Yii::app()->dateFormatter->formatDateTime(strtotime($data->promoted_time))'
//							),
							'promoted_by' => array(
								'header' => 'Appointed By',
								'value' => '$data->promotion_type == "manual" ? $data->promotedBy->name . " @ " . Yii::app()->dateFormatter->formatDateTime(strtotime($data->promoted_time)) : "Automatic"'
							),
							'comments',
							'actions' => array(
								'header' => 'Action',
								'class' => 'CButtonColumn',
								'template' => '{delete}',
								'buttons' => array(
									'delete' => array(
										'label' => 'Delete',
										'url' => '',
										'click' => "js:modules.admin.users.commission.onActionDeleteClick"
									)
								),
								'htmlOptions' => array(
									'style' => 'width: 70px'
								)
							)
						)
					));
					?>
					<div class="controls">
						<a id="promoteUser" class="link-button with-icon no-margin" href="<?php echo $this->createUrl('users/promote', array('id' => $user->id)); ?>">
							<img src="/images/icons/farmfresh/16x16/award_star_gold_3.png" />
							Appoint Agent
						</a>
					</div>
				</div>
			</div>
		</div>
	</div>
<?php endif; ?>
<?php if (Yii::app()->user->checkModuleAccess('Propsage.Accounting.Commission')): ?>
	<div class="grid_16">
		<div id="adjustment-info" class="section">
			<div class="block">
				<div class="form" model-class="ScoreAdjustment">
					<h2>User Adjustments History</h2>
					<?php
					$this->widget('PsGridView', array(
						'id' => 'ScoreAdjustment-grid',
						'dataProvider' => $scoreAdjustmentsDataProvider,
						'emptyText' => 'No Adjustments Found',
						'template' => '{items}{pager}',
						'pagerCssClass' => 'pager float-right',
						'htmlOptions' => array(
							'class' => 'grid-view no-shadow'
						),
						'rowCssClassExpression' => '($row % 2 == 0 ? "even" : "odd")',
						'columns' => array(
							'id' => array(
								'header' => '',
								'name' => 'id',
								'type' => 'raw',
								'value' => 'CHtml::hiddenField("id", $data->id)',
								'headerHtmlOptions' => array(
									'width' => 5,
								)
							),
							'criteria_type_code' => array(
								'header' => 'Score',
								'name' => 'criteria_type_code',
								'value' => '$data->criteriaTypeCode->getCriteriaLabel($data->criteria_type_code)',
								'headerHtmlOptions' => array(
									'width' => 150,
								),
							),
							'amount' => array(
								'name' => 'amount',
								'header' => 'Amount',
								'value' => '$data->criteriaTypeCode->getFormattedValue($data->amount)',
//								'value' => 'Yii::app()->numberFormatter->formatCurrency($user->getNextLevelScores('gross_comm'), "$ ")',
								'headerHtmlOptions' => array(
									'width' => 120,
								),
							),
							'adjustment_effect_time' => array(
								'name' => 'adjustment_effect_time',
								'header' => 'Adjustment Effective On',
								'value' => 'Yii::app()->dateFormatter->formatDateTime(strtotime($data->adjustment_effect_time), "short", "short")',
								'headerHtmlOptions' => array(
									'width' => 160,
								),
							),
							'create_time' => array(
								'name' => 'create_time',
								'header' => 'Adjustment Made On',
								'value' => 'Yii::app()->dateFormatter->formatDateTime(strtotime($data->create_time), "short", "short")',
								'headerHtmlOptions' => array(
									'width' => 160,
								),
							),
							'adjusted_by_user_id' => array(
								'name' => 'adjusted_by_user_id',
								'header' => 'Adjustment Made By',
								'value' => '$data->adjustedByUser->name',
								'headerHtmlOptions' => array(
									'width' => 160,
								),
							),
							'reason' => array(
								'name' => 'reason',
								'header' => 'Created Reason',
								'headerHtmlOptions' => array(
									'width' => 200,
								),
							),
							'actions' => array(
								'header' => 'Action',
								'class' => 'CButtonColumn',
								'template' => '{delete}',
								'buttons' => array(
									'delete' => array(
										'label' => 'Delete',
										'url' => '',
										'click' => 'js:modules.admin.users.commission.onActionDeleteClick'
									)
								),
								'htmlOptions' => array(
									'style' => 'width: 70px',
								)
							)
						)
					));
					?>

					<div class="controls">
						<a id="promoteUser" class="link-button with-icon no-margin" href="<?php echo $this->createUrl('users/adjustScore', array('id' => $user->id)); ?>">
							<img src="/images/icons/fugue/pencil.png" />
							Create Adjustment
						</a>
					</div>
				</div>
			</div>
		</div>
	</div>
<?php endif; ?>

<?php $this->renderPartial('_dialog_delete', array()); ?>
